package com.lemon.controller.lemon;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.lemon.base.result.PageTableRequest;
import com.lemon.model.LemonScripts;
import com.lemon.model.SysMail;
import com.lemon.service.lemon.LemonScriptsService;
import io.swagger.annotations.ApiOperation;
import com.lemon.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import com.lemon.base.result.Results;
import org.springframework.web.servlet.ModelAndView;
import java.util.Map;

/**
 * @author lemon
 */
@Controller
@RequestMapping("/lemonScripts")
@Slf4j
public class LemonScriptsController {

    @Autowired
    private LemonScriptsService lemonScriptsService;

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ApiOperation(value = "保存")
    @ResponseBody
    public Results save(LemonScripts lemonScripts) {
      return  lemonScriptsService.save(lemonScripts);
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id获取")
    public Results get(@PathVariable Long id) {
        return lemonScriptsService.getById(id);
    }

    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ApiOperation(value = "修改")
    public Results  update(LemonScripts lemonScripts) {
       return lemonScriptsService.update(lemonScripts);
    }

    @GetMapping("/listPage")
    @ApiOperation(value = "列表")
    @ResponseBody
    public Results<LemonScripts> list(PageTableRequest request, LemonScripts lemonScripts) {
          request.countOffset();
          Map<String, Object> param = null;
          try {
              param =   StrUtil.convertToMap(lemonScripts);
          } catch (Exception e) {
              e.printStackTrace();
          }
        return lemonScriptsService.getByPage(param, request.getOffset(), request.getLimit());
    }

    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    @ApiOperation(value = "删除")
    @ResponseBody
    public Results delete(LemonScripts lemonScripts) {
        return lemonScriptsService.delete( lemonScripts);
    }

   @ApiOperation(value = "编辑页面", notes = "跳转到菜单信息编辑页面")//描述
   @RequestMapping(value = "/addOrEdit", method = RequestMethod.GET)
   @ResponseBody
    public ModelAndView roleEdit(Model model, HttpServletRequest request, LemonScripts lemonScripts) {
        LemonScripts newLemonScripts = new LemonScripts();
        if(0 != lemonScripts.getId()){
            newLemonScripts = lemonScriptsService.getLemonScriptsById(lemonScripts.getId());
        }
        model.addAttribute("lemonScripts",newLemonScripts);
        ModelAndView modelAndView =  new ModelAndView("lemon/scripts/add-lemon-scripts");
        return modelAndView;
    }

    /**
     * 保存或更新
     */
    @PostMapping(value = "/saveOrUpdate")
    @ApiOperation(value = "保存脚本配置")
    @ApiOperationSupport(author = "lemon")
    @ResponseBody
    public Results saveOrUpdate(LemonScripts lemonScripts) {
        if (null != lemonScripts.getId() && 0 != lemonScripts.getId()) {
            return lemonScriptsService.updateScripts(lemonScripts);
        }
        return lemonScriptsService.saveScripts(lemonScripts);
    }

    @RequestMapping(value = "/run", method = RequestMethod.GET)
    @ApiOperation(value = "执行")
    @ResponseBody
    public Results run(LemonScripts lemonScripts) {
        return lemonScriptsService.run(lemonScripts,"",new String[]{null});
    }

}
